<?php

/**
 * Rola" zadecyduje o uprawnieniach użytkownika. Może przyjąć trzy różne wartości:

    * guest - niezalogowany użytkownik
    * member - zarejestrowany i zalogowany użytkownik
    * admin - administrator

 * @author damian 
 *
 */
//class User extends Zend_Db_Table_Abstract {

class Application_Model_User implements Zend_Auth_Adapter_Interfac
{
	public $login;
	public $password;
	public $role;
	
	
	protected $_users = array(
				        'user1' => array('password' => 'pass1', 'role' => 'member'),
				        'user2' => array('password' => 'pass2', 'role' => 'member'),
				        'user3' => array('password' => 'pass3', 'role' => 'member'),
				        'admin' => array('password' => 'pass4', 'role' => 'admin')
				    );
	
	public function authenticate()
	{
	
		if (!isset($this->_users[$this->login])
				|| $this->_users[$this->login]['password'] != $this->password)
		{
			$result = new Zend_Auth_Result(Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID, null);
		}
		else
		{
			$this->role = $this->_users[$this->login]['role'];
			$result = new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $this);
		}
		
		return $result;
	}

	/*
	 * Logowanie sie
	 */
	public function loginAction()
	{
		$form = new Application_Form_Login();
		$request = $this->getRequest();
		if ($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				$user = new Application_Model_User();
				$user->login = $form->getValue('login');
				$user->password = $form->getValue('password');
				$result = Zend_Auth::getInstance()->authenticate($user);
				if ($result->isValid()) {
					$this->_helper->redirector('index');
				} else {
					$form->login->addError('Podano niepoprawny login i/lub hasło.');
				}
			}
		}
	
		$this->view->loginForm = $form;
	}

	/**
	 * Wylogowanie sie
	 */
	public function logoutAction()
	{
		Zend_Auth::getInstance()->clearIdentity();
		$this->_helper->redirector('index');
	}

}


		